﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>链接</title>

    <link href="../../../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/layui/layui/css/layui.css" rel="stylesheet" />
    <script src="../../../Scripts/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/layui/layui/layui.js" type="text/javascript"></script>
    <script src="../../../Scripts/layui/LayuiDialog.js" type="text/javascript"></script>
    <script src="../../../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../../../Scripts/config.js" type="text/javascript"></script>
    <script src="../../../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../../../Comm/Gener.js" type="text/javascript"></script>
    <script src="../../../Admin/Admin.js"></script>

    <script type="text/javascript">
        $(function () {

            var handler = new HttpHandler("BP.CCBill.WF_CCBill_Admin_DBList");
            handler.AddUrlData();
            var data = handler.DoMethodReturnString("FieldsORM_Init");

            var frmID = GetQueryString("FrmID");
            var dblist = new Entity("BP.CCBill.DBList", frmID);

            dblist.ExpEn = dblist.ExpEn.replace(/~/g, "'");
           
            $("#TB_Doc").val(dblist.ExpEn);

            if (data.indexOf("err@")!=-1) {
                alert(data);
                return;
            }

            if (data == "") {
                return;
            }
            data = JSON.parse(data);
            debugger
            var bakAttrs = new Entities("BP.Sys.MapAttrs");
            bakAttrs.Retrieve("FK_MapData", frmID + "Bak");

            var html = "<table style='width:90%;'>";
            html += "<tr>";
            html += "<th>Idx</th>";
            html += "<th>字段</th>";
            html += "<th>字段名</th>";
            html += "<th>返回类型</th>";
            html += "<th>映射的类型</th>";
            html += "<th>业务类型</th>";
            html += "<th>操作</th>";
            html += "<th>取消映射</th>";
            html += "<th>显示?</th>";
            html += "</tr>";

            for (var i = 0; i < data.length; i++) {
                var en = data[i];
                var barkEn = null;

                for (var idx = 0; idx < bakAttrs.length; idx++) {
                    var bakAttr = bakAttrs[idx];
                    if (en.No === bakAttr.KeyOfEn)
                        barkEn = bakAttr;
                }

                //如果集合里没有，创建一个.
                if (barkEn == null) {

                    barkEn = new Entity("BP.Sys.MapAttr");
                    barkEn.FK_MapData = frmID + "Bak";
                    barkEn.KeyOfEn = en.No;
                    barkEn.Name = en.No;

                    if (en.Name == 'Title')
                        barkEn.Name = "名称";

                    barkEn.DataType = 1; //string类型.

                    if (en.DBType == "Int32" || en.DBType == "Int64" || en.DBType == "Boolean") {
                        barkEn.MyDataType = 2; //int类型.
                    }

                    if (en.DBType == "Decimal" || en.DBType == "Float") {
                        barkEn.MyDataType = 3; //float 类型.
                    }
                    barkEn.Insert();
                }

                var curIdx = i + 1;
                html += "<tr>";
                html += "<td>" + curIdx + "</td>";
                html += "<td>" + barkEn.KeyOfEn + "</td>";


                //把常用的字段转化为 中文.
                if (barkEn.KeyOfEn == barkEn.Name) {
                    if (barkEn.KeyOfEn == 'OID') barkEn.Name = "内部OID";
                    if (barkEn.KeyOfEn == 'BillNo') barkEn.Name = "编号";
                    if (barkEn.KeyOfEn == 'Title') barkEn.Name = "标题";
                    if (barkEn.KeyOfEn == 'Tel') barkEn.Name = "电话";
                    if (barkEn.KeyOfEn == 'Email') barkEn.Name = "邮件";
                    if (barkEn.KeyOfEn == 'Addr') barkEn.Name = "地址";
                    if (barkEn.KeyOfEn == 'RDT') barkEn.Name = "记录日期";
                    if (barkEn.KeyOfEn == 'DeptNo') barkEn.Name = "部门编号";
                    if (barkEn.KeyOfEn == 'DeptT') barkEn.Name = "部门名称";
                }

                var input = "<input type=text value='" + barkEn.Name + "' id='TB_" + barkEn.KeyOfEn + "' name='TB_" + barkEn.KeyOfEn + "' />";

                html += "<td>" + input + "</td>";
                html += "<td>" + en.DBType + "</td>";
                html += "<td>" + GetDataType(barkEn.MyDataType) + "</td>";

                if (barkEn.LGType == 0) html += "<td>普通</td>";
                if (barkEn.LGType == 1) html += "<td>枚举</td>";
                if (barkEn.LGType == 2) html += "<td>外键</td>";


                var oper = "<a href=\"javascript:Edit('" + barkEn.MyPK + "'," + curIdx + ")\">编辑</a>";

                if (barkEn.MyDataType == 1 && IsSysField(en) == false) {
                    oper += " - <a href=\"javascript:TurnToDataType('" + barkEn.MyPK + "',2)\">转成int类型</a>";
                    oper += " - <a href=\"javascript:TurnToFK('" + barkEn.KeyOfEn + "','" + barkEn.FK_MapData + "'," + curIdx + ")\">转成外键</a>";
                    oper += " - <a href=\"javascript:TurnToDataType('" + barkEn.MyPK + "',6)\">转成日期</a>";
                    oper += " - <a href=\"javascript:TurnToDataType('" + barkEn.MyPK + "',7)\">转成日期时间</a>";
                }

                if (barkEn.MyDataType == 2 && barkEn.LGType == 0) {
                    oper += " - <a href=\"javascript:TurnToDataType('" + barkEn.MyPK + "',4)\">转成boolen类型</a>";
                    oper += " - <a href=\"javascript:TurnToEnum('" + barkEn.KeyOfEn + "','" + barkEn.FK_MapData + "'," + curIdx + ")\">转成枚举类型</a>";
                }

                if (barkEn.MyDataType == 2 && barkEn.LGType == 1) {
                    oper += " - <a href=\"javascript:TurnToDataType('" + barkEn.MyPK + "',2)\">转成int类型</a>";
                }

                html += "<td>" + oper + " </td>";

                if (barkEn.LGType != 0 || barkEn.MyDataType == 2 || barkEn.MyDataType == 4 || barkEn.MyDataType == 6 || barkEn.MyDataType == 7) {
                    var oper = "<a href=\"javascript:Del('" + barkEn.MyPK + "')\">取消</a>";
                    html += "<td>" + oper + " </td>";
                } else {
                    html += "<td> 无 </td>";
                }

                if (barkEn.UIVisible == 1)
                    html += "<td><input type=checkbox id='CB_" + barkEn.KeyOfEn + "' name='CB_" + barkEn.KeyOfEn + "' value=1 checked=true /></td>";
                else
                    html += "<td><input type=checkbox id='CB_" + barkEn.KeyOfEn + "' name='CB_" + barkEn.KeyOfEn + "' value=0  /></td>";

                html += "</tr>";
            }
            html += "</table>";

            $("#docs").html(html);
        });



        ///转化为name.
        function IsSysField(en) {

            if (en.KeyOfEn == 'RDT') return true;
            if (en.KeyOfEn == 'OID') return true;
            if (en.KeyOfEn == 'BillNo') return true;
            if (en.KeyOfEn == 'Title') return true;
            if (en.KeyOfEn == 'Tel') return true;
            if (en.KeyOfEn == 'Email') return true;
            if (en.KeyOfEn == 'Addr') return true;
            return false;
        }

        function CheckDT(dt) {
            var err = "";

            for (var i = 0; i < dt.length; i++) {
                var d = dt[i];

            }

            if (dt.Columns.Contains("OID") == false)
                err += "@缺少OID字段,该字段是系统约定的实体主键.";

            if (dt.Columns.Contains("BillNo") == false)
                err += "@缺少 BillNo 字段,该字段是系统约定的 实体编号 .";

            if (dt.Columns.Contains("Title") == false)
                err += "@缺少 Title 字段,该字段是系统约定的 实体标题，或者说实体名称 .";

            if (DataType.IsNullOrEmpty(err) == true)
                return dt;
        }

        function GetDataType(datatype) {
            if (datatype == 1) return "String";
            if (datatype == 2) return "Int";
            if (datatype == 3) return "Float";
            if (datatype == 4) return "Boolean";
            if (datatype == 5) return "Double";
            if (datatype == 6) return "Date";
            if (datatype == 7) return "DateTime";
            if (datatype == 8) return "Money";
        }

        /**
         * 转换成外键、外部数据源
         * @param keyOfEn
         * @param frmID
         * @param idx
         */
        function TurnToFK(keyOfEn, frmID, idx) {
            var url = basePath + "/WF/Admin/FoolFormDesigner/SFList.htm?KeyOfEn=" + keyOfEn + "&FK_MapData=" + frmID;
            var w = parent.window.innerWidth * 4 / 5;
            parent.OpenLayuiDialog(url, "绑定外键/外部数据源", w, 80, "auto", false, false, false, null, function () {
                var en = new Entity("BP.Sys.MapAttr", frmID + "_" + keyOfEn);
                var lab = en.LGType == 0 ? "外部数据源" : "外键";
                //变更这行的数据信息
                var tr = $($("tr")[idx]);
                $(tr.find("td")[5]).html(lab);
                $(tr.find("td")[7]).html("<a href=\"javascript:Del('" + frmID + "_" + keyOfEn + "')\">取消</a>");
            });
        }
        /**
         * 转换成枚举
         * @param mypk
         */
        function TurnToEnum(keyOfEn, frmID, idx) {
            var url = basePath + "/WF/Admin/FoolFormDesigner/SysEnumList.htm?KeyOfEn=" + keyOfEn + "&FK_MapData=" + frmID;
            var w = parent.window.innerWidth * 4 / 5;
            
            parent.OpenLayuiDialog(url, "绑定枚举", w, 80, "auto", false, false, false, null, function () {
                //变更这行的数据信息
                var tr = $($("tr")[idx]);
                $(tr.find("td")[5]).html("枚举");
                $(tr.find("td")[7]).html("<a href=\"javascript:Del('" + frmID + "_" + keyOfEn + "')\">取消</a>");
            });
        }

        function SaveNames() {
            var handler = new HttpHandler("BP.CCBill.WF_CCBill_Admin_DBList");
            handler.AddUrlData();
            handler.AddFormData();
            var data = handler.DoMethodReturnString("FieldsORM_SaveKeysName");
            if (data.indexOf("err@") != -1)
                alert(data);
        }
        function Del(mypk) {

            if (window.confirm('您确定要取消吗？') == false)
                return false;

            var en = new Entity("BP.Sys.MapAttr", mypk);
            en.Delete();
            SaveNames();
            Reload();
        }
        /**
         * 修改字段类型
         * @param mypk MapAttr主键
         * @param dataType 转换的类型
         */
        function TurnToDataType(mypk, dataType) {
            if (window.confirm('您确定要执行吗？如果执行错误，您可以用[取消]来还原。') == false)
                return false;

            var en = new Entity("BP.Sys.MapAttr", mypk);
            en.MyDataType = dataType;
            en.Update();
            SaveNames();
            Reload();
        }

        /**
         * 编辑
         * @param mypk
         */
        function Edit(mypk, idx) {

            var barkEn = new Entity("BP.Sys.MapAttr", mypk);

            var url = "";

            if (barkEn.MyDataType == 1) {
                url = "./En.htm?EnName=BP.Sys.FrmUI.MapAttrString&MyPK=" + mypk;
                if (barkEn.LGType == "0" && barkEn.UIContralType == 1)
                    url = "./En.htm?EnName=BP.Sys.FrmUI.MapAttrSFSQL&MyPK=" + mypk;
                if (barkEn.LGType == "2")
                    url = "./En.htm?EnName=BP.Sys.FrmUI.MapAttrSFTable&MyPK=" + mypk;
            }


            if (barkEn.MyDataType == 2 || barkEn.MyDataType == 3 || barkEn.MyDataType == 5) {
                if (barkEn.MyDataType == 2 && barkEn.LGType == 1 && (barkEn.UIContralType == 1 || barkEn.UIContralType == 3))
                    url = "./En.htm?EnName=BP.Sys.FrmUI.MapAttrEnum&MyPK=" + mypk;
                else
                    url = "./En.htm?EnName=BP.Sys.FrmUI.MapAttrNum&MyPK=" + mypk;
            }

            if (barkEn.MyDataType == 6 || barkEn.MyDataType == 7)
                url = "./En.htm?EnName=BP.Sys.FrmUI.MapAttrDT&MyPK=" + mypk;

            if (barkEn.MyDataType == 4)
                url = "./En.htm?EnName=BP.Sys.FrmUI.MapAttrBoolen&MyPK=" + mypk;
            var w = parent.window.innerWidth*4/5;
            parent.OpenLayuiDialog(url, "编辑字段属性", w, 80, "auto", false, false, false, null, function () {
                var en = new Entity("BP.Sys.MapAttr", mypk);
                //变更这行的数据信息
                var tr = $($("tr")[idx]);
                $(tr.find("td")[7]).html("<a href=\"javascript:Del('" + mypk + "')\">取消</a>");
            });
        }
        /**
         * 获取数据结构
         */
        function Save() {
            var frmID = GetQueryString("FrmID");
            var dblist = new Entity("BP.CCBill.DBList", frmID);
            var doc = $("#TB_Doc").val()
            if (dblist.DBType == 2 && doc.toUpperCase().indexOf("SELECT") != -1) {

            } else {
                if (doc.indexOf('@Key') == -1) {
                    alert("数据源里必须包含@Key参数.");
                    return;
                }
            }
            dblist.ExpEn = doc;
            dblist.Update();
            SaveNames();
            Reload();
        }
        /**
         *修改字段名称、保存应用
         */
        function App() {

            var handler = new HttpHandler("BP.CCBill.WF_CCBill_Admin_DBList");
            handler.AddUrlData();
            handler.AddFormData();
            var data = handler.DoMethodReturnString("FieldsORM_App");
            alert(data);
            Reload();
        }
        function ViewFrm() {
            var frmID = GetQueryString("FrmID");
            var url = "../../../CCForm/FrmGener.htm?FrmID=" + frmID + "&FK_MapData=" + frmID + "&WorkID=0";
            window.open(url);
        }
    </script>
</head>
<body>
    <div id="bar">
    </div>
    <form id="cc">
        <fieldset>
            <legend>单笔记录数据源，必须有：OID,BillNo,Title 字段.</legend>
            <textarea value="" id="TB_Doc" name="TB_Doc" rows="5">
           </textarea>

            <input type="button" value="保存实体数据源" id="Btn_Save" onclick="Save()" />

            <legend>
                字段列表：
            </legend>

            <div id="docs"></div>

            <input type="button" value="提交应用" id="Btn_App" onclick="App()" />

            <input type="button" value="表单预览" id="Btn_ViewFrm" onclick="ViewFrm()" />


            <legend>
                说明：
            </legend>
            <ul>

                <li> 单笔记录数据源也叫实体数据源，他是列表可以看到的数据源。 </li>
                <li> 表达式里必须有一个@Key参数，就是主键。 </li>

                <li> 只有提交应用才能生效 </li>

                <li> 如果字段类型不匹配，请打开字段属性，进行编辑，然后把string类型的转化为其他类型. </li>

                <li>
                    1.使用数据库SQL查询:
                    SELECT A.No as OID, A.No as BillNo, a.Name AS Title, A.Tel,A.Email , B.No as FK_Dept, B.Name AS FK_DeptText
                    FROM Port_Emp A, Port_Dept B WHERE A.FK_Dept=B.No AND A.No='@Key'
                    <br />
                    2.使用URL查询:
                    /DataUser/Handler.ashx?PKVal=@Key
                    <br />
                    &nbsp;&nbsp;①获取到的PKVal为空时，需要返回字段列表的集合，包含字段名称，字段类型 <br />
                    &nbsp;&nbsp;&nbsp;&nbsp;[{"No":"Title","DBType":"string"},"No":"BillNo","DBType":"string"},{"No":"Age","DBType":"int"}]<br />
                    &nbsp;&nbsp;②PKVal不为空时，返回实体集合<br />
                    &nbsp;&nbsp;&nbsp;&nbsp;[{"Title":"测试数据","BillNo":'CS_202204200001',"Age":20}]
                    <br/>
                    3.使用存储过程:
                        如果存在单选单数据的存储过程时必须包含@Key, </br>
                        如果使用Select语句时可以不包含@Key 例如 SELECT '' BillNo,''Title,'' OID ,''Tel,''ZCSJ
                        
                </li>
                <li>
                    <font color="red">注意：SQL语句中字段的别名不能作为查询的条件，需要把某个字段设置成查询条件时需要使用原始字段名，如按照Name查询时不能写成 a.Name as Title</font>
                </li>
            </ul>

        </fieldset>
    </form>

</body>
</html>
